
import { _decorator, Component, Node, Label, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
 
@ccclass('CountDown')
export class CountDown extends Component {

    labelCount: Label = null;
    timeCount: number = 0;
    completedFn: Function = null;

    onLoad() {

    }
    
    setCountDown(times: number, completed: Function) {
        this.unschedule(this._callBack);
        this.timeCount = times;
        this.completedFn = completed;
        this.node.getChildByName('CountDown').getChildByName('Number').getComponent(Label).string = '' + times;
        this.schedule(this._callBack, 1, times);
    }

    clearTimeout() {
        this.unschedule(this._callBack);
    }

    private _callBack() {
        this.labelCount = this.node.getChildByName('CountDown').getChildByName('Number').getComponent(Label);
        this.timeCount = this.timeCount - 1;
        this.labelCount.string = '' + this.timeCount;
        if(this.timeCount < 1) {
            this.unschedule(this._callBack);
            this.labelCount.string = 'GO';
            tween(this.labelCount.node).to(.2, { scale: new Vec3(1.5, 1.5, 1)}).delay(.2).to(.3, { scale: new Vec3(1, 1, 1)}).delay(.3).call(() => {
                if(this.completedFn) {
                    this.completedFn();
                }
            }).start();
        }
    }
}
